/**
 * Copyright © 2018 陈锦韬  All rights reserved.
 * 
 * @Package: com.rx.service.Impl 
 * @author: Administrator   
 * @date: 2018年3月18日 下午10:17:33
 */
package com.rx.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.rx.core.base.BaseDefaultResp;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.exp.BizException;
import com.rx.dto.NetApiVo;
import com.rx.dto.OperVo;
import com.rx.dto.UploadTableFieldVo;
import com.rx.dto.VerVo;
import com.rx.service.NormalDbService;
import com.rx.service.SysCtrlService;

/**
 * @className: SysCtrlServiceImpl 
 * @author: Administrator
 * @Description: TODO
 * @date: 2018年3月18日 下午10:17:33
 */
@Service("SysCtrlServiceImpl")
public class SysCtrlServiceImpl implements SysCtrlService {

	@Override
	public String uploadTableField(UploadTableFieldVo uploadTableFieldVo) throws BizException, Exception {

		NormalDbService normalDbService = new NormalDbService();

		// 清除原数据库字段
		NormalDbService.OperateInfo info= new NormalDbService.OperateInfo();
		info.setOperMethodName("deletePhysical"); 
		info.setOperTableName("rx_db_field");
		normalDbService.setOperateInfo(info);
		
		OperVo operVo = new OperVo();
		operVo.setCondSystemId(uploadTableFieldVo.getSystemId());
		operVo.setTableVer(uploadTableFieldVo.getTableVer());
		normalDbService.process(operVo);
		
		// 插入数据库字段
		info.setOperMethodName("insertBySelective");
		info.setOperTableName("rx_db_field");
		
		normalDbService.processList(uploadTableFieldVo.getListVo());
		
		// 更改版本
		info.setOperMethodName("updateBySelective"); 
		info.setOperTableName("rx_sys_ver");
		normalDbService.process(operVo);
		return new BaseDefaultResp<>().toString();	
	}

	@Override
	public String requestVer(VerVo vo) throws BizException, Exception {
		NormalDbService normalDbService = new NormalDbService();
		
		NormalDbService.OperateInfo info= new NormalDbService.OperateInfo();
		info.setOperMethodName("selectBySelective");
		info.setOperTableName("rx_sys_ver");
		normalDbService.setOperateInfo(info);
		
		VerVo verVo = new VerVo();
		verVo.setSystemId(vo.getSystemId());
		VerVo output = new VerVo();
		List<?> lst = (List<?>) normalDbService.process(verVo).getListArray();
		if(1!=lst.size()){
			throw new BizException(RESULT_CODE.EXCP);
		}
		
		BeanUtils.copyProperties(lst.get(0), output);
		BaseDefaultResp<VerVo> resp =new BaseDefaultResp<>();
		resp.setResultData(output);
		return resp.toString();
	}

	@Override
	public String getNetApi(NetApiVo vo) throws BizException, Exception {
        NormalDbService normalDbService = new NormalDbService();
		
		NormalDbService.OperateInfo info= new NormalDbService.OperateInfo();
		info.setOperMethodName("selectBySelective");
		info.setOperTableName("rx_net_api");
		normalDbService.setOperateInfo(info);
		
		List<?> lst = (List<?>) normalDbService.process(vo).getListArray();		
		BaseDefaultResp<List<?>> resp =new BaseDefaultResp<>();
		resp.setResultData(lst);
		return resp.toString();
	}

	

}
